﻿using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Rs;
using VisionCloud.Domain.DtoModel.Schedule.WorkOrder;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Rs;
using VisionCloud.Service.Schedule;
using VisionCloud.Utility.Extensions;

namespace VisionCloud.Api.Controllers.Schedule
{
    /// <summary>
    /// 工单模块控制器
    /// </summary>
    [SystemModule("工单模块控制器")]
    [ApiVersion("1.0")]
    [Route("api/schedule/[Controller]/[action]")]
    [ApiExplorerSettings(GroupName = "工单模块控制器")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    public class WorkOrderController : ApiBaseController
    {
        public WorkOrderService WorkOrderService { set; get; }

        public RsWorkOrderService RsWorkOrderService { get; set; }

        public RsWorkOrderMapService RsWorkOrderMapService { get; set; }

        /// <summary>
        /// 获取工单树形列表数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetSimpleList(GetSimpleListDto model)
        {
            List<GetSimpleGroupRspDto> data = WorkOrderService.GetSimpleAllList(model);
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = data
            };
        }

        /// <summary>
        /// 获取工单坐标列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetLocationList()
        {
            List<WorkOrderLocationDto> data = WorkOrderService.GetLocaltionList();
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = data
            };
        }

        /// <summary>
        /// 获取工单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetWorkOrderDetail(Guid id)
        {
            WorkOrderDetailDto data = WorkOrderService.GetWorkOrderDetail(id);
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = data
            };
        }

        /// <summary>
        /// 匹配机器人接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> MatchRobot(MatchRobotRqDto request)
        {
            string ip = HttpContext.GetClientUserIp();
            bool result = await WorkOrderService.MatchRobot(request, ip);
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = result
            };
        }

        /// <summary>
        /// 取消匹配机器人接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CancelMatchRobot(MatchRobotRqDto request)
        {
            string ip = HttpContext.GetClientUserIp();
            bool result = await WorkOrderService.CancelMatchRobot(request, ip);
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = result
            };
        }

        /// <summary>
        /// 查询全部工单列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [SwaggerResponse(0, "Result字段为工单列表", typeof(GetAllListResponseModel))]
        public async Task<ApiResult> GetAllList(GetAllListDto dto)
        {
            return await WorkOrderService.GetAllList(dto);
        }

        /// <summary>
        /// 上传工单测量结果
        /// </summary>
        /// <param name="dto"></param>
        /// <remarks>
        /// Code = 0表示成功，其他表示失败
        /// </remarks>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Upload(UploadRequestDto dto)
        {
            return await WorkOrderService.Upload(dto);
        }

        /// <summary>
        /// 工单列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [SwaggerResponse(0, "Data字段为工单列表", typeof(WorkOrderListResponseModel))]
        public async Task<ApiResult> List([FromBody] WorkOrderListRequestDto dto)
        {
            return await TryCatchAsync(RsWorkOrderService.GetPage(dto));
        }

        /// <summary>
        /// 工单列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [SwaggerResponse(0, "工单对象", typeof(WorkOrderListResponseModel))]
        public async Task<ApiResult> GetEntity([FromBody] WorkOrderRequestDto dto)
        {
            return await TryCatchAsync(RsWorkOrderMapService.GetEntity(dto));
        }

        /// <summary>
        /// 根据项目楼栋楼层获取工单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [SwaggerResponse(0, "工单列表", typeof(GetWorkOrderListWithProjectInfoRspDto))]
        public async Task<ApiResult> GetWorkOrderListWithProjectInfo(GetWorkOrderListWithProjectInfoRqDto request)
        {
            return await TryCatchAsync(RsWorkOrderMapService.GetWorkOrderListWithProjectInfo(request));
        }
    }
}